package com.lijiaxing.interceptors;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.lijiaxing.utils.JwtMaker;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

//拦截器
@Component
public class Interceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String x_token = request.getHeader("x-token");
        //所有的请求都会在此被此拦截器调用此函数中为request
        try {
            Map map = JwtMaker.deCoder(x_token);
            //放行
            return true;
        } catch (TokenExpiredException e) {
            response.setStatus(401);
            return false;
        }


    }
}
